﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Sale
{
    public partial class 原料标书录入 : System.Web.UI.Page
    {
        protected string 标书名称;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";
            标书名称 = Request.QueryString["BiaoShuMing"];
            if (!this.IsPostBack)
            {
               
                if (Request.QueryString["y"] != null && Request.QueryString["m"] != null && Request.QueryString["yl"] != null)
                {
                    if (Request.QueryString["list"] == "true")
                    {
                        this.Button_Find.Visible = false;
                        this.Button_Export.Visible = true;
                        this.GridView1.Columns[10].Visible = true;
                    }
                    else
                    {
                        this.GridView1.Columns[10].Visible = true;
                    }
                    this.NianDu.Text = Request.QueryString["y"].ToString();
                    this.NianDu.Enabled = false;
                    this.JiDu.Text = Request.QueryString["m"].ToString();
                    this.JiDu.Enabled = false;
                    this.YuanLiao.Text = Request.QueryString["yl"].ToString();
                    this.YuanLiao.Enabled = false;
                      
                }
                else
                {
                    this.NianDu.Text = DateTime.Today.Year.ToString();
                    this.GridView1.Columns[10].Visible = true;
                }
            
            }
        }
        protected void Button_Save_Click(object sender, EventArgs e)
        {
            int 原料标书名ID = int.Parse(Request.QueryString["BiaoShuID"]);
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            try
            {
                //if (this.NianDu.Enabled)
                //{
                if (this.ZhaoBiaoLiang.Text == "")
                {
                    this.Message.Text = "计划采购量不能为空!";
                    return;
                }
                else if (this.DanJia.Text == "")
                {
                    this.Message.Text = "单价不能为空!";
                    return;
                }

                Cmd.CommandText =
"if EXISTS(select * from 原料中标书 where 年度=" + this.NianDu.Text + " and 季度=" + this.JiDu.Text + " and 原料ID=" + this.YuanLiao.Text + " and 供货单位ID=" + this.GongHuoShang.Text + ")\n"
+ "begin\n"
+ "return\n"
+ "end\n"
+ "else\n"
+ "begin\n"
+ @"insert into 原料中标书 values('" + this.NianDu.Text + "','" + this.JiDu.Text + "','" + YuanLiao.Text + "','" + this.GongHuoShang.Text + "','" + 原料标书名ID + "','" + double.Parse(this.ZhaoBiaoLiang.Text) + "','" + double.Parse(this.DanJia.Text) + "','" + DateTime.Today.ToShortDateString() + "',default,''"
+ ")\n"
+ "end";
                //  }          

                if (Cmd.ExecuteNonQuery() < 1)
                {
                    if (this.NianDu.Enabled)
                    {
                        this.Message.Text = "·保存失败，该记录已存在现有招标文件中";
                        return;
                    }
                    else
                    {
                        this.Message.Text = "·保存失败，该记录已存在现有招标文件中";
                        return;
                    }
                }
                else
                {
                    this.Message.Text = "·保存成功";
                }
            }
            catch (Exception ex)
            {
                this.Message.Text = "·保存失败 " + ex.Message;
                return;
            }
            Conn.Close();
            Cmd.Dispose();
            Conn.Dispose();
            Button_Query_Click(null, null);
            this.ZhaoBiaoLiang.Text = "";
            this.DanJia.Text = "";
            this.Panel3.Visible = true;
       
            
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            int 原料标书名ID = int.Parse(Request.QueryString["BiaoShuID"]);
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "select count(*) from 原料中标书 where 原料标书名ID=" + 原料标书名ID;
            if ((int)Cmd.ExecuteScalar() == 0)
            {
                this.SqlDataSource1.SelectCommand = "";
            }
            else
            {
                string sql = "";
                if (this.NianDu.Text != "0")
                {
                    sql = " where 原料标书名ID=" + 原料标书名ID;
                }
                sql = "select 年度,季度,原料ID,供货单位ID,品名,规格,单位,名称,数量,原料中标书.单价,日期"
                    + " from 原料中标书 inner join 原料 on 原料中标书.原料ID=原料.ID inner join 原料供货单位 on 原料供货单位.ID =原料中标书.供货单位ID" + sql + " order by 年度 desc,季度 asc";
                this.SqlDataSource1.SelectCommand = sql;
            }
            this.GridView1.SelectedIndex = -1;
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {
                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p + 1;
                    }
                }
                else
                {
                    TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
                }
            }

        }
        protected void Button_Add_Click(object sender, EventArgs e)
        {
         
            this.Button_Save.Visible = false;
            this.Panel3.Visible = false;
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
        }    

        protected void Button_Find_Click(object sender, EventArgs e)
        {
            Button_Query_Click(null, null);
            this.GridView1.Columns[10].Visible = false;
        }

        protected void Button_Export_Click(object sender, EventArgs e)
        {

        }
    }
}
